package vn.edu.saigontech.ncms.web.bookstore;

import java.util.Collection;
import java.util.Date;
import java.util.Set;

import vn.edu.saigontech.ncms.model.bookstore.ImportReceipt;
import vn.edu.saigontech.ncms.model.bookstore.ImportReceiptDetail;
import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.ImportReceiptDetailService;
import vn.edu.saigontech.ncms.service.ImportReceiptService;
import vn.edu.saigontech.ncms.service.ItemService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class ImportAction extends BaseAction {
	private Long id;
	
	// INFO
	private String number;
	private String date;
	private Long userId;
	private String comment;
	
	// DETAILS
	private int quantity;
	private Long itemId;
	private Long detailId;
	
	// REDIRECT
	private Long programId;
	private Long levelId;
	
	private ImportReceipt importReceipt;
	private Collection<ImportReceipt> imports;

	private Set<ImportReceiptDetail> details;
	
	private Collection<User> users;
	private Collection<Program> programs;

	public String showCreate() {
		this.users = UserService.INSTANCE.findByRole("Book Manager");
		return SUCCESS;
	}
	
	public String create() {
		ImportReceipt importReceipt = new ImportReceipt();
		importReceipt.setNumber(this.number);
		Date date = DateUtil.fromString(this.date, this.i18n("format.date"));
		importReceipt.setDate(date);
		importReceipt.setComment(this.comment);
		User user = UserService.INSTANCE.findById(this.userId);
		importReceipt.setUser(user);
		
		importReceipt = ImportReceiptService.INSTANCE.persist(importReceipt);
		
		// to re-direct to show edit items
		this.id = importReceipt.getId();
		return SUCCESS;
	}
	
	public String showEdit() {
		this.importReceipt = ImportReceiptService.INSTANCE.findById(this.id);
		this.users = UserService.INSTANCE.findByRole("Book Manager");
		return SUCCESS;
	}
	
	public String edit() {
		this.importReceipt = ImportReceiptService.INSTANCE.findById(this.id);
		importReceipt.setNumber(this.number);
		Date date = DateUtil.fromString(this.date, this.i18n("format.date"));
		importReceipt.setDate(date);
		importReceipt.setComment(this.comment);
		User user = UserService.INSTANCE.findById(this.userId);
		importReceipt.setUser(user);
		importReceipt = ImportReceiptService.INSTANCE.persist(importReceipt);
		
		return SUCCESS;
	}
	
	public String list() {
		this.imports = ImportReceiptService.INSTANCE.findAll();
		return SUCCESS;
	}
	
	public String showEditDetails() {
		this.importReceipt = ImportReceiptService.INSTANCE.findById(this.id);
		if(this.importReceipt != null) {
			this.details = this.importReceipt.getDetails();
		}
		this.programs = ProgramService.INSTANCE.findAll();
		return SUCCESS;
	}
	
	public String addDetail() {
		this.importReceipt = ImportReceiptService.INSTANCE.findById(this.id);
		
		ImportReceiptDetail detail = new ImportReceiptDetail();
		Item item = ItemService.INSTANCE.findById(this.itemId);
		detail.setItem(item);
		detail.setQuantity(this.quantity);
		detail.setImportReceipt(this.importReceipt);
		
		item.increaseQuantity(this.quantity);
		
		detail = ImportReceiptDetailService.INSTANCE.persist(detail);
		return SUCCESS;
	}
	
	public String deleteDetail() {
		ImportReceiptDetail detail = ImportReceiptDetailService.INSTANCE.findById(this.detailId);
		
		Item item = detail.getItem();
		item.decreseQuantity(detail.getQuantity());
		
		ImportReceiptDetailService.INSTANCE.deleteById(this.detailId);
		this.id = detail.getImportReceipt().getId();
		return SUCCESS;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ImportReceipt getImportReceipt() {
		return importReceipt;
	}

	public void setImportReceipt(ImportReceipt importReceipt) {
		this.importReceipt = importReceipt;
	}

	public Collection<User> getUsers() {
		return users;
	}

	public void setUsers(Collection<User> users) {
		this.users = users;
	}

	public Collection<Program> getPrograms() {
		return programs;
	}

	public void setPrograms(Collection<Program> programs) {
		this.programs = programs;
	}

	public Set<ImportReceiptDetail> getDetails() {
		return details;
	}

	public void setDetails(Set<ImportReceiptDetail> details) {
		this.details = details;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Collection<ImportReceipt> getImports() {
		return imports;
	}

	public void setImports(Collection<ImportReceipt> imports) {
		this.imports = imports;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Long getDetailId() {
		return detailId;
	}

	public void setDetailId(Long detailId) {
		this.detailId = detailId;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public Long getLevelId() {
		return levelId;
	}

	public void setLevelId(Long levelId) {
		this.levelId = levelId;
	}
}
